extends Global

var old_options = null	# 原选项按钮
var home = null # 游戏主选项界面
var show_faci_list = [] # 需要显示的设施
var forBoxMap = true
var ibtn = preload("res://tscn/base/imgBtn.tscn")
var pop_wd = null	# 弹窗界面
func _in():
	sys.game.connect("onToMap",self,"_startSetting")

func _startSetting(mapId):
	if sys.game.mode == "map":
		old_options = sys.mapScene.get_node("%obFaci")
		old_options.hide()
		if not old_options.get_parent().has_node("faciInfoBtn"):
			var utils = sys.get_node("VEBase").utils
			home = sys.game.get_node("ui/home/RtsBar/btnBox")
			var texture = load("res://!ve/base/InformationAggregate/ico_informat_ui.png")
			var bt = add_imgBtn(old_options.get_parent(), texture," ","5","设施信息(F)")
			bt.rect_position = old_options.rect_position
			bt.connect("pressed",self,"_load_faci_window")
			bt.name = "faciInfoBtn"
			bt.rect_scale = Vector2(0.6, 0.6)
			utils.addShotCutToBtn(bt, KEY_F, false)
			# home.rect_position = home.rect_position - Vector2(40,0)
			# home.move_child(bt,0)
		

func _load_faci_window():
	var arrs = []
	for i in sys.mapScene.objs.items: 
		if i.visible && i.isCs && !i.invalid:
			arrs.append(i)
	show_faci_list = arrs
	window_pop()

func window_pop():
	pop_wd = sys.newDlg("res://!ve/base/InformationAggregate/faciBaseDlg.tscn")
	pop_wd.setTile("设施信息")
	pop_wd.set_tabs(["全部","城镇","商店","副本","功能"])
	
	for f in show_faci_list:
		var img = data.newRes(f.icoId)
		var txt = "%s(%d级)" % [f.name,f.lv] if f.lv > 0 else f.name
		var bt = add_imgBtn(pop_wd.get_faci_gd(0),img,txt)
		bt.connect("pressed",self,"transmit",[f.cell])
		if f is FaciTown:
			bt = add_imgBtn(pop_wd.get_faci_gd(1),img,txt)
			bt.connect("pressed",self,"transmit",[f.cell])

		elif f is FaciBuy:
			bt = add_imgBtn(pop_wd.get_faci_gd(2),img,txt)
			bt.connect("pressed",self,"transmit",[f.cell])

		elif f is FaciDungeon:
			bt = add_imgBtn(pop_wd.get_faci_gd(3),img,txt)
			bt.connect("pressed",self,"transmit",[f.cell])

		elif f.lv <= 0:
			bt = add_imgBtn(pop_wd.get_faci_gd(4),img,txt)
			bt.connect("pressed",self,"transmit",[f.cell])
#		bt.rect_scale = Vector2(0.5, 0.5)
#		bt.rect_size = Vector2(12, 12)

func add_imgBtn(father,img,txt="",name="imgBtn",dec=""):
	var ib = ibtn.instance()
	ib.setImg(img)
	ib.name = name
	if txt: ib.setText(txt)
	if dec: sys.addTip(ib,dec)
	father.add_child(ib)
	return ib

func transmit(cell:Vector2):
	pop_wd.del()
	sys.mapScene.playFaci.matMoveUp(cell)
